import "./index.css"
// import "dist/sweetalert.min.js"
// import "dist/sweetalert.css"

// var xhr = new XMLHttpRequest();

// xhr.open("post", "http://127.0.0.1:5500/#list", true);
// xhr.onreadystatechange = function() {
//     if (xhr.readyState == 4 && xhr.status == 200) {
//         let text = xhr.responseText;
//         console.log(JSON.parse(text));
//     }
// }
// xhr.send();

var btn = document.querySelector('button');

var username = document.querySelector('#username');
var password = document.querySelector('#password');
var name = document.querySelector('#name');
var group = document.querySelector('#group');

function checkNum(obj) {
    var num = obj.value;

    if (num.match(/[0-9]{8}$/) == null || num.length != 8) {
        return false;
    }
    return true;
}

function checkCode(obj) {
    var code = obj.value;

    if (code.match(/[\s\S*]{6,16}$/) == null || code.length > 16) {
        return false;
    }
    return true;
}

function checkIsChinese(input) {
    var reg = /^[\u4e00-\u9fa5]+$/;
    var len = input.length;
    if (!reg.test(input)) {
        return false;
    }
    return true;
}

username.addEventListener("blur", function() {
    var usernameBox = document.querySelector('#usernameBox');
    if (!checkNum(username)) {
        username.style.borderColor = 'red';
        if (username.value == '') {
            usernameBox.innerText = "学号不能为空！";
        } else {
            usernameBox.innerText = "请输入正确的学号！";
        }
    } else {
        username.style.borderColor = ' #c8cccf';
        usernameBox.innerText = "";
    }
})

username.addEventListener("keydown", function(e) {
    // 按下回车键且输入框值非空时
    if (e.keyCode == 13 && username.value) {
        password.focus();
    }
})

password.addEventListener("blur", function() {
    var passwordBox = document.querySelector('#passwordBox');
    if (!checkCode(password)) {
        password.style.borderColor = 'red';
        if (password.value == '') {
            passwordBox.innerText = "密码不能为空！";
        } else {
            passwordBox.innerText = "请输入6-16位任意字符！";
        }
    } else {
        password.style.borderColor = ' #c8cccf';
        passwordBox.innerText = "";
    }
})

password.addEventListener("keydown", function(e) {
    // 按下回车键且输入框值非空时
    if (e.keyCode == 13 && password.value) {
        name.focus();
    }
})

name.addEventListener("blur", function() {
    var nameBox = document.querySelector('#nameBox');
    if (!checkIsChinese(name.value)) {
        name.style.borderColor = 'red';
        if (name.value == '') {
            nameBox.innerText = "姓名不能为空！";
        } else {
            nameBox.innerText = "请输入中文！";
        }
    } else {
        name.style.borderColor = ' #c8cccf';
        nameBox.innerText = "";
    }
})

name.addEventListener("keydown", function(e) {
    // 按下回车键且输入框值非空时
    if (e.keyCode == 13 && name.value) {
        group.focus();
    }
})

group.addEventListener("blur", function() {
    var groupBox = document.querySelector('#groupBox');
    if (group.value == '') {
        group.style.borderColor = 'red';
        groupBox.innerText = "请选择组别！";
    } else {
        group.style.borderColor = ' #c8cccf';
        groupBox.innerText = "";
    }
})

btn.addEventListener('click', function() {
    alert("正在申请注册，请稍候...");
})